// 设计获奖信息编辑页面

// 关闭VSCode非必要报错
// ignore_for_file: camel_case_types, non_constant_identifier_names, must_be_immutable, prefer_interpolation_to_compose_strings

import 'package:flutter/material.dart';
import 'package:project_award_statistics/utils/dialog_yaru_design/add_award_info/dialog_addawardinfo.dart';
import 'package:project_award_statistics/utils/dialog_yaru_design/delete_award_info/dialog_delete_award_info.dart';
import 'package:project_award_statistics/utils/load_award_info_from_db/award_model.dart';
import 'package:project_award_statistics/utils/load_award_info_from_db/load_award_info.dart';
import 'package:project_award_statistics/utils/my_yaru_buttons/my_yaru_button_error_info.dart';
import 'package:window_manager/window_manager.dart';
import 'package:yaru/yaru.dart';

class ProfileEditPage_Desktop extends StatefulWidget {
  String stu_name;    // 获取当前用户(学生)姓名
  ProfileEditPage_Desktop({
    super.key,
    required this.stu_name,
  });

  @override
  State<ProfileEditPage_Desktop> createState() => _ProfileEditPage_DesktopState();
}

class _ProfileEditPage_DesktopState extends State<ProfileEditPage_Desktop> with TickerProviderStateMixin {
  // 初始化当前学生姓名变量
  late String stu_name;
  List <award_model> user_award_list=List.empty();    // 初始化等下会从数据库中获取的学生信息

  // 初始化动画控制总线
  late AnimationController _animationController_widget1;  // 初始化动画总线1
  late AnimationController _animationController_widget2;  // 初始化动画总线2
  // 覆写父类构造函数
  @override
  void initState () 
    {
      super.initState();
      // 从父类传入当前学生姓名
      stu_name=widget.stu_name;

      // 覆写页面更新时行为
      // sideMenu.addListener((index){
      //  pageController.jumpToPage(index);
      // });

      // 初始化动画总线
      _animationController_widget1 = AnimationController(
        duration: Duration(seconds: 1), // 设置动画持续时间
        vsync: this,
      );
      _animationController_widget2 = AnimationController(
        duration: Duration(milliseconds: 1300), // 设置动画持续时间
        vsync: this,
      );     

      // 动画,启动!
      _animationController_widget1.forward().then((_){
        // 动画总线1启动之后启动动画总线2
        _animationController_widget2.forward();
      });  
      _loadAwardList();   // 执行函数更新user_award_list成员列表对象
    }
  Future <void> _loadAwardList() async
    {
      List <award_model> awards = await DBProvider_AwardInfo().get_award_list(widget.stu_name);  
      setState(() {   //更新变量状态
        user_award_list=awards;
      });
    }
  void animation_dispose ()
    {
      // 停止并释放对象
      _animationController_widget1.stop();
      _animationController_widget1.dispose();
      _animationController_widget2.stop();
      _animationController_widget2.dispose();
    }

  // 覆写父类析构函数
  @override
  void dispose ()
    {
      animation_dispose();
      super.dispose();
    }

  // 当右下角加号被触发(也就是要增加获奖信息)显示确认增加对话框 
  Future <void> ShowDialog_AddAwardProfile () async 
    {
      await showDialog(
        context: context, 
        builder: (context)
          {
            return Dialog_AddAwardInfo(
              stu_name: stu_name,
            );
          }
      );
      List <award_model> awards = await DBProvider_AwardInfo().get_award_list(widget.stu_name);
      setState(() {   // 更新变量状态,及时进行列表刷新
        user_award_list=awards;
      });
    }

  // 按下删除键执行函数调出对话框询问用户是否需要进行删除操作
  Future <void> ShowDialog_DeleteAwardInfo (String award_name,String award_time) async 
    {
      await showDialog(
        context: context, 
        builder: (context){
          return Dialog_DeleteAwardInfo(
            stu_name: stu_name,
            award_time:award_time,
            award_name:award_name,
          );
        },
      );
      List <award_model> awards = await DBProvider_AwardInfo().get_award_list(widget.stu_name);
      setState(() {   // 更新变量状态,及时进行列表刷新
        user_award_list=awards;
      });
    }
  
  @override
  Widget build(BuildContext context) {

    // 更改窗口标题文字
    WindowManager.instance.setTitle("个人获奖信息查询  --   获奖信息编辑");

    // 初始化程序框架
    return Scaffold(
      floatingActionButton: Padding(   // 设置右下角的'+'号
        padding: const EdgeInsets.only(bottom: 15.0,right: 15.0),
        child: FloatingActionButton(
          onPressed: ShowDialog_AddAwardProfile,
          backgroundColor: YaruColors.adwaitaGreen,
          child: Icon(Icons.add),
        ),
      ),
      body: Padding(    // 设定程序所有UI离边界的距离
        padding: const EdgeInsets.only(top:15.0,left: 25.0,right: 50.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            SlideTransition(
              position: Tween<Offset>(
                begin: Offset(-1.2, 0),
                end: Offset.zero,
              ).animate(
                CurvedAnimation(
                  parent: _animationController_widget1,
                  curve: Curves.easeInOut, // 设置特定动画曲线
                ),
              ),
              child:Text(
                "在大学生涯中,您一共获得了以下奖项:",
                style: TextStyle(
                  fontSize: 30,
                ),
              ),
            ),
            const SizedBox(height: 30,),   // 设置合理的控件间距离
            Flexible( 
              child: SlideTransition( 
                position: Tween<Offset>(   // 设置动画启停时坐标
                  begin: Offset(-1.1,0),
                  end: Offset.zero,
                ).animate(
                  CurvedAnimation(
                    parent: _animationController_widget2, 
                    curve: Curves.easeInOut,
                  ),
                ),  // position定位
                child:user_award_list.isEmpty    // 判断用户有无获奖奖项,而显示不同的
                  ?Center(
                    child: Text(
                      "哎呀,您的奖项列表空空如也 ~",
                      style: TextStyle(
                        color:YaruColors.warmGrey,
                        fontSize: 18,
                      ),
                    ),
                  )
                  :ListView.builder(
                    itemCount: user_award_list.length,
                    itemBuilder:(context,index) {
                      return Container(
                        margin: EdgeInsets.symmetric(vertical: 10),   // 设置容器外之间的边距
                        decoration: BoxDecoration(
                          color: YaruColors.titleBarLight,
                          borderRadius: BorderRadius.circular(12),
                        ), 
                        child: Padding(    // Padding用于设置下方文字离有色Container容器的左/右边距
                          padding: const EdgeInsets.only(left:18.0,right: 18.0),
                          child: Row(   // 水平布局让文字与右侧的删除图标平行
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              // const SizedBox(width: 1,),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  SizedBox(height: 10,),  //设置上边界
                                  Text(
                                    "奖项名称: ${user_award_list[index].award_name}",
                                    style: TextStyle(
                                      fontSize: 20,
                                    ),
                                  ), 
                                  Text(
                                    "地域等级: ${user_award_list[index].region_level}",
                                    style: TextStyle(
                                      fontSize: 20,
                                    ),
                                  ),
                                  Text(
                                    "获奖等第: ${user_award_list[index].award_level}",
                                    style: TextStyle(
                                      fontSize: 20,
                                    ),
                                  ),
                                  Text(
                                    "获奖时间: ${user_award_list[index].award_time}年",
                                    style: TextStyle(
                                      fontSize: 20,
                                    ),
                                  ),
                                  SizedBox(height: 10,),  //设置下边界
                                ],
                              ),
                                      
                              SizedBox(    // 设置每行末尾的按钮大小
                                height: 40,
                                width: 100,
                                child: MyYaruButton_ErrorInfo(
                                  text: "删除", 
                                  onPressed: (){   // 按下删除按钮时显示对话框
                                    ShowDialog_DeleteAwardInfo(
                                      user_award_list[index].award_name,
                                      user_award_list[index].award_time,
                                    );
                                  },
                                ),
                              ),
                            ],
                          ),
                        ),
                      );
                    },
                  ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
